<?php
// 本类由系统自动生成，仅供测试用途
class IndexAction extends CommonAction {
    public function index()
    {
      $this->users = M('User')->select();
      $this->user_class = "active";
      $this->right = "user";
      $this->display('index');
      // p($this->users);
    }

    public function addUser()
    {
      $this->addUser_class = "active";
      $this->right = "addUser";
      $this->display('index');
    }
    
    /**
     * 添加用户处理函数
     */
    public function addUserHandle()
    {
      if(!IS_POST) halt("页面错误！");
      $data['username'] = I("username");
      $data['password'] = encode('123456');
      $data['email'] = I("email");
      $data['status'] = I("status");
      // $data['birthday'] = I("birthday");
      // $data['qq'] = I("qq");
      $data['regtime'] = date("Y-m-d H:i:s");
      $data['regip'] = get_client_ip();
      // p($data);
      // p(D('User')->isUserExist($data['username']));

      // 判断用户名是否存在
      if (D('User')->isUserExist($data['username'])) {
        $this->error("用户名已存在！");
      } else {
        if (D("User")->addUser($data)) {
          $this->success('添加用户成功！', U(GROUP_NAME . '/Index/index'));
        } else {
          $this->error('添加用户失败！');
        }
      }
      
      // $this->display('index');
    }

    /**
     * 删除用户处理函数
     * @return [type] [description]
     */
    public function delUserHandle()
    {
      // if(!IS_POST) halt("页面错误！");
      $u_id = I('u_id');
      $user = D('User')->selectUserByUid($u_id);
      // p($user);
      if ($user['username'] == C('SUPER_ADMIN_NAME')) {
        $this->error('不允许删除超级用户');
      }
      
      if (D('User')->delUserByUid($u_id)) {
        $this->success('删除用户成功！', U(GROUP_NAME . '/Index/index'));
      } else {
        $this->error('删除用户失败！');
      }
    }

    /**
     * 对用户的权限操作
     * @return [type] [description]
     */
    public function updateUserHandle()
    {
      if(!IS_POST) halt("页面错误！");
      $data['u_id'] = I('u_id');
      $data['status'] = I('status');

      if (D('User')->updateUser($data)) {
        $this->success('更新用户权限成功!', U(GROUP_NAME . '/Index/index'));
      } else {
        $this->error('更新用户权限失败！');
      }
      
    }

    // 角色列表
  public function role()
  {
    $this->role_class = 'active';
    $this->right = 'role';
    $this->role = M('Role')->select();
    // p(M('Role')->select());
    $this->display('index');
  }

  // 添加角色
  public function addRole()
  {
    $this->addRole_class = 'active';
    $this->right = 'addRole';
    $this->display('index');
  }

  // 添加角色表单处理
  public function addRoleHandle()
  {
    if (M('Role')->add($_POST)) {
      $this->success('添加成功！', U(GROUP_NAME . '/Index/role'));
    } else {
      $this->error('添加失败！');
    }
  }

  // 删除角色
  public function delRole()
  {
    if (M('Role')->where('id='.I('id'))->delete()) {
      $this->success('删除成功！', U(GROUP_NAME . '/Index/role'));
    } else {
      $this->error('删除失败！');
    }
  }

  // 配置角色权限
  public function access()
  {
    $id = I('id', '', 'intval');
    $node = M('Node')->order('sort')->field('id, name, title, pid')->select();
    $access = M('Access')->where(array('role_id' => $id))->getField('node_id', true);
    // p($access);
    $this->node = node_merge($node, $access);
    // p($this->node);
    $this->rid = $id;
    $this->role_class = 'active';
    $this->right = 'access';
    $this->display('index');
  }

  // 配置权限表单处理函数
  public function accessHandle()
  {
    $access = I('post.access');
    $role_id = I('post.rid');

    // 删除原先权限
    M('Access')->where(array('role_id' => $role_id))->delete();
    $data = array();

    foreach ($access as $a) {
      $a = explode('_', $a);
      $data[] = array(
          'role_id' => $role_id,
          'node_id' => $a[0],
          'level'   => $a[1]);
    }
    if (M('Access')->addAll($data)) {
      $this->success('更新成功！', U(GROUP_NAME . '/Index/access', array('id' => $role_id)));
    } else {
      $this->error('更新失败！');
    }

  }

  // 节点列表
  public function node()
  {
    $this->node_class = 'active';
    $this->right = 'node';

    $field = 'id, name, pid, title';
    $this->node = node_merge( M('Node')->order('sort')->field($field)->select() );

    // p( $this->node );
    $this->display('index');
  }

  // 添加节点
  public function addNode()
  {
    $this->pid = I('pid', '0', 'intval'); 
    $this->level = I('level', '1', 'intval');
    $this->addNode_class = 'active';
    $this->right = 'addNode';
    switch ($this->level) {
      case 1:{
        $this->type = '应用';
        break;
      }
      case 2:{
        $this->type = '控制器';
        break;
      }
      case 3:{
        $this->type = '方法';
        break;
      }
    }
    $this->display('index');
  }

  /**
   * 添加节点表单处理
   */
  public function addNodeHandle()
  {
    if (M('Node')->add($_POST)) {
      $this->success('添加成功！', U(GROUP_NAME . '/Index/node'));
    } else {
      $this->error('添加失败！');
    }
  }

  // 删除节点
  public function delNode()
  {
    $id = I('id');
    $temp = M('Node')->field('pid, level')->where('id=' . $id)->select();
    $node = $temp[0];
    switch ($node['level']) {
      // 应用
      case 1: {
        // 获得应用对应的 action
        $action = M('Node')->where('pid=' . $id)->field('id, pid')->select();

        foreach ($action as $a) {
          // 获得每个 action 对应的方法
          $method = M('Node')->where('pid=' . $a['id'])->field('id')->select();

          // 删除每个方法在 access 表内的对应数据
          foreach ($method as $m) {
            M('Access')->where('node_id=' . $m['id'])->delete();
          }

          // 删除每个 action 对应的方法
          M('Node')->where('pid=' . $a['id'])->delete();

          // 删除每个 action 在 access 表内对应的数据
          M('Access')->where('node_id=' . $a['id'])->delete();
        }

        // 删除所有的 action
        M('Node')->where('pid=' . $id)->delete();
        break;
      }
      // 控制器要删除对应的方法
      case 2: {
        // 获得 action 对应的方法
        $method = M('Node')->where('pid=' . $id)->field('id')->select();

        // 删除每个方法在 access 表内的对应数据
        foreach ($method as $m) {
          M('Access')->where('node_id=' . $m['id'])->delete();
        }

        // 删除 action 对应的方法
        M('Node')->where('pid=' . $id)->delete();
        break;
      }
      // 方法直接删除
      // case 3: {
        
      //   break;
      // }
      
      default:
        break;
    }

    if (M('Node')->where('id=' . $id)->delete() && M('Access')->where('node_id=' . $id)->delete()) {
      $this->success('删除成功！', U(GROUP_NAME . '/Index/node'));
    } else {
      // $this->error('删除失败！');
      $this->display('index');
    }
}

  // 修改节点
  public function modifyNode()
  {
    // p($_GET);
    $id = I('id', '', 'intval');
    $temp = M('Node')->where('id='.$id)->select();
    $this->node = $temp[0];
    $this->node_class = 'active';
    $this->right = 'modifyNode';
    switch (I('level')) {
      case 1:{
        $this->type = '应用';
        break;
      }
      case 2:{
        $this->type = '控制器';
        break;
      }
      case 3:{
        $this->type = '方法';
        break;
      }
    }
    $this->display('index');
  }

  // 修改节点表单处理
  public function modifyNodeHandle()
  {
    if (M('Node')->save($_POST)) {
      $this->success('更新成功！', U(GROUP_NAME . '/Index/node'));
    } else {
      $this->error('更新失败！');
    }
  }

  // 用户角色页面
  public function userRole()
  {
    $this->user_class = 'active';
    $this->right = 'roleUser';
    $temp = M('PersonBase')->field('p_id, p_name')->where('p_id=' . I('id', '', 'intval'))->select();    
    $this->user = $temp[0];
    // p($this->user);
    $this->roleUser = M('RoleUser')->join('lazy_role on lazy_role_user.role_id = lazy_role.id')
                               ->where('user_id=' . I('id', '', 'intval'))
                               ->field('role_id, name')
                               ->select();
    // p($this->roleUser);
    $this->role = M('Role')->select();
     // p($this->role);
    $this->display('index');
  }

  // 配置用户角色
  public function modifyUserRole()
  {
    $p_id = I('post.p_id');
    $role = I('post.role_id');
    // p($role);
    $data = array();
    foreach ($role as $r) {
      $data[] = array(
          'user_id' => $p_id,
          'role_id' => $r);
    }
    if (M('RoleUser')->addAll($data)) {
      $this->success('添加用户角色成功！', U(GROUP_NAME . '/Index/userRole', array('id' => $p_id )));
    } else {
      $this->error('添加用户角色失败！');
    }
  }

  public function delRoleUser()
  {
    $data = explode('_', I('roleUser'));
    if (M('RoleUser')->where("user_id=$data[1] and role_id=$data[0]")->delete()) {
      $this->success('删除用户角色成功！', U(GROUP_NAME . '/Index/userRole', array('id' => $data[1])));
    } else {
      $this->error('删除用户角色失败！');
    }
  }


}